<?php
require_once(dirname(__FILE__) . "/../config.php");
require_once('weixin.class.php');
session_start();
$weixin = new class_weixin();
$array = array();
if (!isset($_GET["code"])) { //访问我就去微信服务器登录
  $redirect_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  $jumpurl = $weixin->oauth2_authorize(urlencode($redirect_url), "snsapi_userinfo", "123");
  Header("Location: $jumpurl");
  exit;
} else { //回调了    
  $tz = trim($_REQUEST['tz']);		
  $back_act = empty($tz) ?  '/index.php' : urldecode($tz);
  $access_token_oauth2 = $weixin->oauth2_access_token($_GET["code"]);
  $user_info = $weixin->oauth2_get_user_info($access_token_oauth2['access_token'], $access_token_oauth2['openid']);
  $openid = $user_info['openid'];
  if (empty($openid)) { //空的说明是手动访问这个页面
    //跳到登录页           
    require_once(DEDEINC . "/datalistcp.class.php");
    $dlist = new DataListCP();
    $query = "";
    $dlist->SetTemplate(dirname(__FILE__) . "/../templets/weixin-bangdin.htm");
    $dlist->SetParameter("a", "485"); //自定义分页GET参数 
    $dlist->pageSize = 20;
    $dlist->SetSource($query);
    $dlist->Display();
    exit;
  }
  $nickname = $user_info['nickname'];
  $face =  str_replace("http://", "https://", $user_info['headimgurl']);
  if ($user_info["sex"] == '1') {
    $sex = '男';
  } else {
    $sex = '女';
  }
  $mtype = '个人';
  $dfscores = 0;
  $dfmoney = 0;
  $dfrank = $dsql->GetOne("SELECT money,scores FROM `#@__arcrank` WHERE rank='10' ");
  if (is_array($dfrank)) {
    $dfmoney = $dfrank['money'];
    $dfscores = $dfrank['scores'];
  }
  $jointime = $logintime = time();
  $joinip =  $loginip = GetIP();
  $spaceSta = 0;
  $_SESSION['jj_openid'] = $openid;
  $_SESSION['jj_nickname'] = $nickname;
  $_SESSION['jj_face'] = $face;
  $_SESSION['jj_sex'] = $sex;
  //作者：覃小华 2020年6月24日21:37:06
  $uarr = $dsql->GetOne("select * from #@__member where `jjwxopenid` = '$openid'");
  if (is_array($uarr)) { //存在即登陆
    $cfg_ml->PutLoginInfo($uarr['mid']);
    $cfg_ml->DelCache($uarr['mid']);
    header("Location:".$back_act);
    exit;
  } else { //不存在 看用户是否已登录
       //如果已经登录了
       if (!empty($cfg_ml->M_ID)) {
      //绑定微信 
      $sql = "update #@__member set `jjwxopenid` = '" . $_SESSION['jj_openid'] . "',`uname` = '" . $_SESSION['jj_nickname'] . "',`face` = '" . $_SESSION['jj_face'] . "',`sex` = '" . $_SESSION['jj_sex'] . "' where mid = '" . $cfg_ml->M_ID . "' ";
      $dsql->ExecuteNoneQuery($sql);
      $_SESSION['jj_openid'] = '';
      $_SESSION['jj_nickname'] = '';
      $_SESSION['jj_face'] = '';
      $_SESSION['jj_sex'] = '';
      ShowMsg("微信绑定成功...", $back_act, 0, 1000);
      exit;
    } else { //注册新用户
      $userid = 'wx' . rand(10000000, 99999999);
      $pp = "pwd";
      if (function_exists('password_hash')) {
      $pp = "pwd_new";
      $pwd = password_hash($userpwd, PASSWORD_BCRYPT);
      } else {
      $pwd = md5($userpwd);
      }
      //推荐人
      $tjrmid = GetCookie('tjrmid');
      if (empty($tjrmid)) {
        $tjrmid = 0;
      }
      $inQuery = "INSERT INTO `#@__member` (`mtype` ,`userid` ,`uname`,`$pp`  ,`sex` ,`rank` ,`money`  ,`scores` , `matt`,
	    `spacesta` ,`face`,`safequestion`,`safeanswer` ,`jointime` ,`joinip` ,`logintime` ,`loginip`,`jjwxopenid`,`tjrmid` )    
		   VALUES ('$mtype','$userid','$nickname','$pwd','$sex','10','$dfmoney','$dfscores',     
         '0','$spaceSta','$face','$safequestion','$safeanswer','$jointime','$joinip','$logintime','$loginip','$openid','$tjrmid'); ";
      $dsql->ExecuteNoneQuery($inQuery);
      $mid = $dsql->GetLastID();
      $space = 'person';
      $membertjquery = "INSERT INTO `#@__member_tj` (`mid`,`article`,`album`,`archives`,`homecount`,`pagecount`,`feedback`,`friend`,`stow`)
                   VALUES ('$mid','0','0','0','0','0','0','0','0'); ";
      $dsql->ExecuteNoneQuery($membertjquery);
      $spacequery = "INSERT INTO `#@__member_space`(`mid` ,`pagesize` ,`matt` ,`spacename` ,`spacelogo` ,`spacestyle`, `sign` ,`spacenews`)
				VALUES('{$mid}','10','0','{$uname}的空间','','$space','',''); ";
      $dsql->ExecuteNoneQuery($spacequery);
      $dsql->ExecuteNoneQuery("INSERT INTO `#@__member_flink`(mid,title,url) VALUES('$mid','DedeBIZ','https://www.dedebiz.com'); ");
      $cfg_ml->PutLoginInfo($mid);
      $cfg_ml->DelCache($mid);
      header("Location:".$back_act);
    }
  }
  exit();
}